package loginserver.clientpackets;

import java.security.GeneralSecurityException;
import javax.crypto.Cipher;


/**
 * Format: x
 * 0 (a leading null)
 * x: the rsa encrypted block with the login an password
 */
public class RequestAuthLogin extends ClientBasePacket
{

	private byte[] _raw = new byte[128];
	private String _user;
	private String _password;
	private int _ncotp;


	public void readImpl()
	{

	}

	public void run()
	{
		byte[] decrypted = null;
		try
		{
			Cipher rsaCipher = Cipher.getInstance("RSA/ECB/nopadding");
//			rsaCipher.init(Cipher.DECRYPT_MODE, getClient().getRSAPrivateKey());
			decrypted = rsaCipher.doFinal(_raw, 0x00, 0x80 );
		}
		catch (GeneralSecurityException e)
		{
			e.printStackTrace();
			return;
		}

		_user = new String(decrypted, 0x5E, 14 ).trim();
		_user = _user.toLowerCase();
		set_password(new String(decrypted, 0x6C, 16).trim());
		_ncotp = decrypted[0x7c];
		_ncotp |= decrypted[0x7d] << 8;
		_ncotp |= decrypted[0x7e] << 16;
		_ncotp |= decrypted[0x7f] << 24;
	}

	@Override
	protected void writeImpl() {
		writeC(0x00);
		writeB(_raw);
		
	}

	public void set_password(String _password) {
		this._password = _password;
	}

	public String get_password() {
		return _password;
	}
	
}
